package com.butuh.uang.tuh.ang.util;

import android.content.Context;

import com.butuh.uang.tuh.ang.listener.OnClickNextListener;
import com.butuh.uang.tuh.ang.model.BaseResult;
import com.butuh.uang.tuh.ang.net.Api;

import java.util.ArrayList;
import java.util.List;

import cn.droidlover.xdroidmvp.log.XLog;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import okhttp3.RequestBody;

public class OnClickLogUtils {

    public static void onClickPermute(Context context, String yemianName, String djArea, int index) {
        String uuid = RandomUtil.getUUID1();
        List<String> maps = new ArrayList<>();
        maps.add(uuid);
        maps.add(yemianName);
        maps.add(djArea);
        maps.add(String.valueOf(index));
        onClickLog(context, "permute", JsonUtils.getRequestBody(maps), 0,null);
    }


    public static void onClickLog(Context context, String name, String message, int bianhao) {
        String strEntry = "[[\"" + name + "\",\"" + message + "\"," + System.currentTimeMillis() + "," + bianhao + "]]";
        OnClickLogFaceFireUtil.getInstance(context).setAnalytics(name, message, bianhao);
        RequestBody body = JsonUtils.strToBody(strEntry);
        Api.getNetService().onClickLog(JsonUtils.getAllParmarts(context), body)
                .compose(XApi.<BaseResult>getApiTransformer())
                .compose(XApi.<BaseResult>getScheduler())
                .subscribe(new ApiSubscriber<BaseResult>() {
                    @Override
                    public void onNext(BaseResult stBean) {
                        XLog.e("--token--" + stBean.toString());
                    }

                    @Override
                    protected void onFail(NetError error) {
                        XLog.e("========" + error.getType() + "=====" + error.getMessage());
                    }
                });
    }


    public static void onClickLog(Context context, String name, String message, int bianhao, final OnClickNextListener onClickNextListener) {
        String strEntry = "[[\"" + name + "\",\"" + message + "\"," + System.currentTimeMillis() + "," + bianhao + "]]";
        OnClickLogFaceFireUtil.getInstance(context).setAnalytics(name, message, bianhao);
        RequestBody body = JsonUtils.strToBody(strEntry);
        Api.getNetService().onClickLog(JsonUtils.getAllParmarts(context), body)
                .compose(XApi.<BaseResult>getApiTransformer())
                .compose(XApi.<BaseResult>getScheduler())
                .subscribe(new ApiSubscriber<BaseResult>() {
                    @Override
                    public void onNext(BaseResult stBean) {
                        XLog.e("--token--" + stBean.toString());
                        if (null != onClickNextListener) {
                            onClickNextListener.clickNext();
                        }
                    }

                    @Override
                    protected void onFail(NetError error) {
                        XLog.e("========" + error.getType() + "=====" + error.getMessage());
                    }
                });
    }

}
